import json
import os
from pathlib import Path
from asgiref.sync import async_to_sync
from django.http.response import JsonResponse
from rest_framework.views import APIView
from http import HTTPStatus

from .serializers import (
  AnarchyQpSerializer,
  AntonymsQpSerializer,
  ExecuteCodeQpSerializer,
  HolidaysQpSerializer,
  SynonymsQpSerializer
)
from .services import (
  execute_code,
  find_holidays,
  get_antonyms,
  get_synonyms,
  get_random_anekdot,
  make_anarchy_text,
)

class AnekdotView(APIView):
  @async_to_sync
  async def get(self, request):
    '''
    Gets random anekdot
    '''
    anekdot = await get_random_anekdot()

    return JsonResponse({'result': anekdot})

class AnarchyView(APIView):
  '''
    Makes text in anarchy style
  '''
  def get(self, request):
    serializer = AnarchyQpSerializer(data=request.query_params)

    if not serializer.is_valid():
      return JsonResponse(
        status=HTTPStatus.BAD_REQUEST,
        data={'errors': serializer.errors}
      )

    anarchy_text = make_anarchy_text(serializer.validated_data['text'])

    return JsonResponse({'result': anarchy_text})


class SynonymsView(APIView):
  @async_to_sync
  async def get(self, request):
    '''
    Finds synonym for word
    '''
    serializer = SynonymsQpSerializer(data=request.query_params)

    if not serializer.is_valid():
      return JsonResponse(
        status=HTTPStatus.BAD_REQUEST,
        data={'errors': serializer.errors}
      )

    result = await get_synonyms(serializer.validated_data['word'])

    return JsonResponse({'result': result})


class AntonymsView(APIView):
  @async_to_sync
  async def get(self, request):
    '''
    Finds antonym for word
    '''
    serializer = AntonymsQpSerializer(data=request.query_params)

    if not serializer.is_valid():
      return JsonResponse(
        status=HTTPStatus.BAD_REQUEST,
        data={'errors': serializer.errors}
      )

    result = await get_antonyms(serializer.validated_data['word'])

    return JsonResponse({'result': result})

class HolidaysView(APIView):
  def get(self, request):
    '''
    Finds holidays by day and month
    '''
    serializer = HolidaysQpSerializer(data=request.query_params)

    if not serializer.is_valid():
      return JsonResponse(
        status=HTTPStatus.BAD_REQUEST,
        data={'errors': serializer.errors}
      )

    result = find_holidays(
      serializer.validated_data['day'],
      serializer.validated_data['month'],
    )

    return JsonResponse({'result': result})

class CodeExecutionView(APIView):
  @async_to_sync
  async def get(self, request):
    '''
    Executes code on prorramming language
    '''
    serializer = ExecuteCodeQpSerializer(data=request.query_params)

    if not serializer.is_valid():
      return JsonResponse(
        status=HTTPStatus.BAD_REQUEST,
        data={'errors': serializer.errors}
      )

    result = await execute_code(
      serializer.validated_data['language'],
      serializer.validated_data['code'],
    )

    return JsonResponse({'result': result})
